seatrows = 0;
seatcols = 0;

jQuery(document).ready(function(){
	var chkupxx = jQuery('#book-seat').length;
	seatrows = jQuery.trim(jQuery('#seat-rows').val());
	seatcols = jQuery.trim(jQuery('#seat-cols').val());

	jQuery('body').on('click','.seat', function(){
		var sid = jQuery(this).attr('id');
		toggle_seat_selection(sid);
	});

	if(chkupxx==0){

		jQuery('body').on('mouseover', '.row-selector', function(){
			var rid = jQuery(this).parent().addClass('selectable');
		});

		jQuery('body').on('mouseout', '.row-selector', function(){
			var rid = jQuery(this).parent().removeClass('selectable');
		});

		jQuery('body').on('mouseover', '.row-deselector', function(){
			jQuery(this).parent().addClass('deselectable');
		});

		jQuery('body').on('mouseout', '.row-deselector', function(){
			jQuery(this).parent().removeClass('deselectable');
		});

		jQuery('body').on('click', '.row-selector', function(){
			var xid = jQuery(this).attr('act');
			jQuery(this).addClass('row-deselector');
			jQuery(this).removeClass('row-selector');
			jQuery(this).parent().addClass('deselectable');
			jQuery(this).parent().removeClass('selectable');
			jQuery('#row-'+xid+' .seat').removeClass('selected');
			jQuery('#row-'+xid+' .seat').removeClass('deselected');
			jQuery('#row-'+xid+' .seat').addClass('selected');
			for(var x=1;x<=seatcols;++x){
				jQuery('#enabled-'+xid+'-'+x).attr('value', '1');
			}
		});

		jQuery('body').on('click', '.row-deselector', function(){
			var xid = jQuery(this).attr('act');
			jQuery(this).addClass('row-selector');
			jQuery(this).removeClass('row-deselector');
			jQuery(this).parent().addClass('selectable');
			jQuery(this).parent().removeClass('deselectable');
			jQuery('#row-'+xid+' .seat').removeClass('deselected');
			jQuery('#row-'+xid+' .seat').removeClass('selected');
			jQuery('#row-'+xid+' .seat').addClass('deselected');
			for(var x=1;x<=seatcols;++x){
				jQuery('#enabled-'+xid+'-'+x).attr('value', '0');
			}
		});

		jQuery('body').on('mouseover', '.col-deselector, .col-selector', function(){
			var cid = jQuery(this).attr('act');
			for(var v=1;v<=seatrows;++v){
				jQuery('#'+v+'-'+cid).addClass('seat-hovered');
			}
		});

		jQuery('body').on('mouseout', '.col-deselector, .col-selector', function(){
			var cid = jQuery(this).attr('act');
			for(var v=1;v<=seatrows;++v){
				jQuery('#'+v+'-'+cid).removeClass('seat-hovered');
			}
		});

		jQuery('body').on('click', '.col-deselector', function(){
			jQuery('.seat').removeClass('seat-hovered');
			var cid = jQuery(this).attr('act');
			for(var v=1;v<=seatrows;++v){
				jQuery('#'+v+'-'+cid).removeClass('selected');
			}
			jQuery('.col-deselector[act="'+cid+'"]').addClass('col-selector');
			jQuery('.col-deselector[act="'+cid+'"]').removeClass('col-deselector');
		});

		jQuery('body').on('click', '.col-selector', function(){
			jQuery('.seat').removeClass('seat-hovered');
			var cid = jQuery(this).attr('act');
			for(var v=1;v<=seatrows;++v){
				jQuery('#'+v+'-'+cid).addClass('selected');
			}
			jQuery('.col-selector[act="'+cid+'"]').addClass('col-deselector');
			jQuery('.col-selector[act="'+cid+'"]').removeClass('col-selector');
		});

		jQuery('body').on('mouseover', '.seat-all-toggle, .seat-all-toggled', function(){
			jQuery(this).parent().parent().addClass('all-hovered');
		});

		jQuery('body').on('mouseout', '.seat-all-toggle, .seat-all-toggled', function(){
			jQuery('.all-hovered').removeClass('all-hovered');
		});

		jQuery('body').on('click', '.seat-all-toggled', function(){
			jQuery('.seat').removeClass('selected');
			jQuery('.seat-all-toggled').addClass('seat-all-toggle');
			jQuery('.seat-all-toggled').removeClass('seat-all-toggled');
			jQuery('.row-deselector').removeClass('row-deselector').addClass('row-selector');
			jQuery('.col-deselector').removeClass('col-deselector').addClass('col-selector');
		});

		jQuery('body').on('click', '.seat-all-toggle', function(){
			jQuery('.seat').addClass('selected');
			jQuery('.seat-all-toggle').addClass('seat-all-toggled');
			jQuery('.seat-all-toggle').removeClass('seat-all-toggle');
			jQuery('.row-selector').removeClass('row-selector').addClass('row-deselector');
			jQuery('.col-selector').removeClass('col-selector').addClass('col-deselector');
		});

	} // end big if statement

	jQuery('body').on('click', '.close-floating-box', function(){
		jQuery('body').removeClass('hide-overflow');
		jQuery('.floating-box').remove();
	});

	jQuery("body").on('focus blur', '.color-picker', function(){
		var xid = jQuery(this).attr('id');
		var idnum = xid.replace('color', '');
		jQuery('#color_picker_color'+idnum).slideToggle();
	});

}); // end document ready

function toggle_seat_selection(x){
	var chk = jQuery('#'+x+'.selected').length;
	var chkup = jQuery('#book-seat').length;
	var pclass = jQuery('#class-'+x).val();
	if( chkup==1 ){
		if((pclass=='seat-disabled' || pclass=='no-seats' || pclass=='seat-booked') ){
			return false;
		}else if( pclass=='seat-available' ){
			var seatname = jQuery('#'+x).attr('seatname');
			var seatid = 'seat-'+jQuery('#'+x).attr('id');
			var seatRegion = $('#selectedSeatRegion');
			if( seatRegion.find('#'+seatid).length > 0 ){
				seatRegion.find('#'+seatid).remove();
				var ticketCount = seatRegion.find('span[id]').length;
			}else{
				var ticketCount = seatRegion.find('span[id]').length;
				if( ticketCount >= 4 ){
					alert('同一订单选票不能超过4张！');
					return false;
				}else{
					ticketCount++;
					seatRegion.append('<span id='+seatid+'><em>'+seatname+'</em><b>&nbsp;</b></span>');
				}
			}
			var price = $('#moviePrice').html();
			$('#selectSeatCount').html( ticketCount );
			$('#movieAmount').html( ticketCount * price );
		}
	}
	if(chk==0){
		jQuery('#'+x).addClass('selected');
		jQuery('#enabled-'+x).attr('value', '1');
	} else {
		jQuery('#'+x).removeClass('selected');
		jQuery('#enabled-'+x).attr('value', '0');
	}

}


function generate_grid(){
	seatrows = jQuery.trim(jQuery('#seat-rows').val());
	seatcols = jQuery.trim(jQuery('#seat-cols').val());
	if( seatrows=='' || parseInt(seatrows,10)<=0 ){
		alert('请在横排输入框输入正确的数字！');
		return false;
	}
	if( seatcols=='' || parseInt(seatcols,10)<=0 ){
		alert('请在竖排输入框输入正确的数字！');
		return false;
	}
	var out='';
	var chkupxx = jQuery('#book-seat').length;

	if(chkupxx==1){
		var ttyy='';
	} else {
		var ttyy='seat-available';
	}

	for(var i=1;i<=seatrows;++i){
		out=out+'<div class="seat-row" id="row-'+i+'">';
		for(var j=1;j<=seatcols;++j){
			var gnum = i*j;
			out=out+'<div class="seat '+ttyy+'" id="'+i+'-'+j+'">\
			<input type="text" value="seat-available" id="class-'+i+'-'+j+'" name="seat['+i+']['+j+']" />\
			</div>';
		}
		out=out+'</div>';
	}

	jQuery('.seat-grid').html(out);
//    jQuery('.seat-grid').css('width', (j+1)*35+'px');

	for(var y=1;y<=i;++y){
		jQuery('#row-'+y).prepend('<div class="row-selector" act="'+y+'">'+y+'</div>');
	}

	var csel='<div class="seat-all-toggle">全选</div>';
	for(var z=1;z<j;++z){
		csel = csel+'<div class="col-selector" act="'+z+'">'+z+'</div>';
	}
	jQuery('.seat-grid').prepend('<div class="blank-row">'+csel+'</div>');
}

/*
function generate_grid(){
	seatrows = jQuery('#seat-rows').val();
	seatcols = jQuery('#seat-cols').val();
	var out='';
	chkupxx = jQuery('#book-seat').length;

	if(chkupxx==1){
		var ttyy='';
	} else {
		var ttyy='selected';
	}
	var colstype = jQuery("input[name='cols-type'][checked]").val();
	console.log(colstype);
	var character = 'ABCDEFGHIGKLMNOPQRSTUVWXYZ';

	for(i=0;i<seatrows;++i){
		out=out+'<div class="seat-row" id="row-'+i+'">';
		for(j=0;j<seatcols;++j){
			gnum = i*j;
			out=out+'<div class="seat '+ttyy+'" id="'+character[i]+'-'+j+'">\
			<input type="text" value="1" id="class-'+i+'-'+j+'" name="seat['+i+'-'+j+'][class]" />\
			</div>';
		}
		out=out+'</div>';
	}

	jQuery('.seat-grid').html(out);
	jQuery('.seat-grid').css('width', (j+1)*46+'px');

	for(y=0;y<i;++y){
		if(colstype=='2'){
			colvaule = character[y];
		}else{
			colvaule = y;
		}
		jQuery('#row-'+colvaule).prepend('<div class="row-deselector" act="'+colvaule+'">'+colvaule+'</div>');
	}

	var csel='<div class="seat-all-toggled">ALL</div>';
	for(z=0;z<j;++z){
		csel = csel+'<div class="col-deselector" act="'+(z+1)+'">'+(z+1)+'</div>';
	}
	jQuery('.seat-grid').prepend('<div class="blank-row">'+csel+'</div>');
}
*/


function mark_seats(){
	if( jQuery('.seat.selected').length == 0 ){
		alert('请先选中要操作的座位！');
		return false;
	}
	
	//var stype = jQuery('#seat-marker').val();
	var stype = $("input[name='seat-marker']:checked").val();
	console.log(stype);

	// jQuery('#seat-marker option').each(function(index){
	// 	var aval = jQuery(this).val();
	// 	jQuery('.seat.selected').removeClass(aval);
	// });
	
	jQuery("input[name='seat-marker']").each(function(index){
		var aval = jQuery(this).val();
		jQuery('.seat.selected').removeClass(aval);
	});
	
	//循环操作选中的座位
	jQuery('.seat.selected').each(function(index){
		var sid = jQuery(this).attr('id');
		jQuery(this).addClass(stype);
		jQuery('#class-'+sid).attr("value", stype);
		jQuery('#enabled-'+sid).attr("value", "0");
	});
	jQuery('.row-deselector').addClass('row-selector');
	jQuery('.row-deselector').removeClass('row-deselector');
	jQuery('.seat.selected').removeClass('selected');

	jQuery('.seat-all-toggled').addClass('seat-all-toggle');
	jQuery('.seat-all-toggled').removeClass('seat-all-toggled');
}

//设置座位类型
function mark_seats000(){
	var stype = $("input[name='seat-marker']:checked").val();
	console.log('stype:'+stype);
	
	//尝试循环seatMap
	var oriMapArr = dataBase["seatMap"];
	console.log(oriMapArr);
	var newMapArr = new Array();
	for(var i=1;i<=initCols;i++){
		//console.log(i)
		var colStr = oriMapArr[i-1];
		var colArr = colStr.split('');
		var tmpStr = '';
		for(var j=1;j<=initRows;j++){
			if(stype){
				if(selectSeats.indexOf(i+'_'+j) == -1){
					tmpStr += colArr[j-1];
				}else{
					tmpStr += stype;
				}
			}
		}
		console.log(tmpStr);
		newMapArr.push(tmpStr);
	}
	//console.log(newMapArr);
	//console.log(selectSeats);
	dataBase["seatMap"] = newMapArr;
	
	var param = {
		specId : specId,
		rows : initRows,
		cols : initCols,
		stype : stype
	};
	
	param.selectSeats = JSON.stringify(selectSeats);
	
	$.post(url_saveSpecSeats, param, function(ret){
		if(ret.code == 1){
			Wei.Util.wapTipNew({msg: ret.msg, icon: 1, data: ret.data}, function(){
				layer.closeAll();
				//Wei.Util.readyToJump(ret);
			});
			initSeats();
		}else if(ret.code == 0){
			Wei.Util.wapTipNew({msg: ret.msg, icon: 2, data: ret.data});
		}
	},'json');
	
	// var map = new Array();
	// var seatCell = $(".seatCharts-cell");
	// seatCell.each(function() {
	// 	var mapId = $(this).attr("id");
	//
	// 	var checkFlag = $(this).hasClass("seat-available");
	// 	//console.log('checkFlag:'+checkFlag)
	// 	if(checkFlag == true){
	// 		seatData1.push(mapId);
	// 	}
	//
	// 	var checkFlag = $(this).hasClass("seat-disabled");
	// 	//console.log('checkFlag:'+checkFlag)
	// 	if(checkFlag == true){
	// 		seatData2.push(mapId);
	// 	}
	//
	// 	var checkFlag = $(this).hasClass("seat-blank");
	// 	//console.log('checkFlag:'+checkFlag)
	// 	if(checkFlag == true){
	// 		seatData3.push(mapId);
	// 	}
	// })
	// dataBase["seatMap"] = map;
	
	//使用横排数列来循环
	// var rows = $('#seat-rows').val();
	// var cols = $('#seat-cols').val();
	// var x = cols;
	// var y = rows;
	// var map = new Array();
	// for(var i=1;i<=x;i++){
	// 	var tmp = "";
	// 	for(var j=1;j<=y;j++){
	// 		if(stype == 'seat-disabled'){
	// 			if(selectSeats.indexOf(i+'_'+j) == -1){
	// 				console.log('1');
	// 				tmp += "a";
	// 			}else{
	// 				console.log('2');
	// 				tmp += "d";
	// 			}
	// 		}else if(stype == 'seat-blank'){
	// 			if(selectSeats.indexOf(i+'_'+j) == -1){
	// 				console.log('3');
	// 				tmp += "a";
	// 			}else{
	// 				console.log('4');
	// 				tmp += "b";
	// 			}
	// 		}else{
	// 			console.log('5');
	// 			tmp += "a";
	// 		}
	// 	}
	// 	map.push(tmp);
	// }
	// dataBase["seatMap"] = map;
	
	// var map = [];
	// for(var i=0;i<x;i++){
	// 	map[i]="";
	// 	for(var j=0;j<y;j++){
	// 		if(stype == 'no-seats'){
	// 			var tmpI = i+1;
	// 			var tmpJ = j+1;
	// 			if(selectSeats.indexOf(tmpI+'_'+tmpJ) == -1){
	// 				console.log('1');
	// 				map[i]+="a";
	// 			}else{
	// 				console.log('2');
	// 				map[i]+="_";
	// 			}
	// 		}else{
	// 			console.log('3');
	// 			map[i]+="a";
	// 		}
	// 	}
	// }
	
}
//==========提交表单(废弃)
$('.saveSeats000').on('click', function(e){
	var param = {};
	param.hid = $('#J_hid').val();
	param.rows = $('#seat-rows').val();
	param.cols = $('#seat-cols').val();
	
	var seatData = new Array();

	var seatCell = $(".seatCharts-cell");
	//把五种座位类型，post过去
	seatCell.each(function() {
		var mapId = $(this).attr("id");
		
		// var checkFlag = $(this).hasClass("seat-available");
		// //console.log('checkFlag:'+checkFlag)
		// if(checkFlag == true){
		// 	seatData1.push(mapId);
		// }
		//
		// var checkFlag = $(this).hasClass("seat-disabled");
		// //console.log('checkFlag:'+checkFlag)
		// if(checkFlag == true){
		// 	seatData2.push(mapId);
		// }
		//
		// var checkFlag = $(this).hasClass("seat-blank");
		// //console.log('checkFlag:'+checkFlag)
		// if(checkFlag == true){
		// 	seatData3.push(mapId);
		// }

		var tmp = new Array();
		
		//用户循环的方法来取座位会遇到一个二维数组的问题，很是麻烦，所以在上面直接保存数据
		//带颜色的座位
		for (var prop in priceList) {
			var item = priceList[prop];
			var checkFlag = $(this).hasClass(item['classes']);
			//console.log('checkFlag:'+checkFlag)
			if(checkFlag == true){
				tmp.push(mapId);
			}
		}
		seatData.push(tmp);
	})
	console.log(seatData);
	
	param.seatData1 = JSON.stringify(seatData1);
	param.seatData2 = JSON.stringify(seatData2);
	param.seatData3 = JSON.stringify(seatData3);
	
	$.post(url_saveSeats, param, function(ret){
		if(ret.code == 1){
			Wei.Util.wapTipNew({msg: ret.msg, icon: 1, data: ret.data}, function(){
				layer.closeAll();
				//Wei.Util.readyToJump(ret);
			});
		}else if(ret.code == 0){
			Wei.Util.wapTipNew({msg: ret.msg, icon: 2, data: ret.data});
		}
	},'json');
});